import java.util.*;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/26
 * Time:18:51
 **/
public class Main2 {
    /**
     * 每日一题-跳石板
     *
     * @param args
     */
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            Queue<Integer> queue = new LinkedList<>();
            HashMap<Integer, Integer> map = new HashMap<>();
            ArrayList<Integer> list = new ArrayList<>();

            queue.add(n);
            map.put(n, 0);
            while (!queue.isEmpty()) {
                int head = queue.poll();
                list = getAppNums(head);
                if (head == m) {
                    System.out.println(map.get(head));
                    return;
                }
                for (int i = 0; i < list.size(); i++) {
                    if (!map.containsKey(head + list.get(i)) && head + list.get(i) <= m) {
                        queue.add(head + list.get(i));
                        map.put(head + list.get(i), map.get(head) + 1);
                    }
                }
            }
            System.out.println(-1);
        }
    }

    /**
     * 求约数
     *
     * @param head
     * @return
     */
    private static ArrayList<Integer> getAppNums(int head) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 2; i <= Math.sqrt(head); i++) {
            if (head % i == 0) {
                list.add(i);
                if (head / i != i) {
                    list.add(head / i);
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] step = new int[m + 1];
        for (int i = 0; i < m + 1; i++) {
            step[i] = Integer.MAX_VALUE;
        }
        step[n] = 0;
        for (int i = n; i < m; i++) {//i代表当前石板编号
            if (step[i] == Integer.MAX_VALUE) {
                continue;
            }
            List<Integer> list = getAppNums(i);
            for (int j : list) {//j代表一次可以跳多少石板
                if (i + j <= m && step[i + j] != Integer.MAX_VALUE) {
                    step[i + j] = Math.min(step[i + j], step[i] + 1);
                    //step[i+j]表示跳到下一个石板的步数，与他本身和上一个石板步数+1 作比较，求最小
                } else if (i + j <= m) {
                    step[i + j] = step[i] + 1;
                    //如果step[i+j] == Integer.MAX_VALUE,说明他本身没有步数，直接+1
                }
            }
        }
        if (step[m] == Integer.MAX_VALUE) {//说明到不了m个石板
            System.out.println(-1);
        } else {
            System.out.println(step[m]);
        }
    }

}
